import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.util.*;

public class SaveLoad {
	private static String password = "DerpWerp";
	private static String filename = "sav";
	public static void save(){
		try{
            FileOutputStream saveFile = new FileOutputStream(filename+".txt");
            ObjectOutputStream save = new ObjectOutputStream(saveFile);
            save.writeObject(main.player);
            File txt = new File(filename+".txt");
            txt.delete();}
		catch(Exception fail){
			fail.printStackTrace();}

	}
	
	public static void load(){
		
	}
	
	public static void Encryption(boolean de) throws Exception {
        FileInputStream inFile;
        FileOutputStream outFile;
        if (de==false){
   inFile = new FileInputStream(filename+".txt");
   outFile = new FileOutputStream(filename+".sav");
        }
        else{
         inFile = new FileInputStream(filename+".sav");
             outFile = new FileOutputStream(filename+".txt");
        }

   PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
   SecretKeyFactory keyFactory =
       SecretKeyFactory.getInstance("PBEWithMD5AndDES");
   SecretKey passwordKey = keyFactory.generateSecret(keySpec);


   byte[] salt = new byte[8];
   if (de==false){
   Random rnd = new Random();
   rnd.nextBytes(salt);}
   else
       inFile.read(salt);
   int iterations = 100;

   PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, iterations);

   Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
   if (de==false){
   cipher.init(Cipher.ENCRYPT_MODE, passwordKey, parameterSpec);
   outFile.write(salt);
   }
   else
   cipher.init(Cipher.DECRYPT_MODE, passwordKey, parameterSpec);


  
   byte[] input = new byte[64];
   int bytesRead;
   while ((bytesRead = inFile.read(input)) != -1)
   {
      byte[] output = cipher.update(input, 0, bytesRead);
      if (output != null) outFile.write(output);
   }

   byte[] output = cipher.doFinal();
   if (output != null) outFile.write(output);

   inFile.close();
   outFile.flush();
   outFile.close();

	}

}
